{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d8e077e5",
   "metadata": {},
   "source": [
    "--------\n",
    "* 项目日期：2021-12-23\n",
    "* week17\n",
    "* 项目人：戴祥哲\n",
    "* 项目名称：名字全称&元音字母查询app项目\n",
    "* 项目语言：python+Flask\n",
    "* 参考文档:\n",
    "> 1. [Flask](https://dormousehole.readthedocs.io/en/latest/quickstart.html#id2)\n",
    "> 2. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35323403",
   "metadata": {},
   "source": [
    "* 项目的核心知识点：、\n",
    "> 1. Flask角色定位： 中央登记中心\n",
    ">> 1. 路由：\n",
    ">>> 1. url 相对路径(不完整的路径)：相对于 http://127.0.0.1:5000 的相对路径\n",
    ">>> 2. HTTP方法：【GET 默认】【POST 用户输入】\n",
    ">> 2. 视图函数：\n",
    ">>> 1. 书写函数的过程(if elif for while etc.)处理数据\n",
    ">>> 2. return(返回值)：text文本、HTML文件、JSON数据文件 etc.\n",
    "> 2. HTML模板渲染\n",
    ">> 1. Python文件 ---> HTML文件 ---> render_template() ---> from flask import render_template\n",
    ">> 2. 在 期末项目.ipynb文件 同一个路径下 新建一个 templates文件夹专门存放 页面文件\n",
    ">> 3. render_template('HTML 文件名')\n",
    "> 3. [form表单](https://www.w3school.com.cn/html/html_forms.asp)\n",
    ">> 1. 用于收集用户输入：HTML文件 ---> Python文件 ---> 指定1：method；指定2：action\n",
    ">> 2. input: text 输入框 ： submit 提交按钮\n",
    ">> 3. 一般是POST请求，需注意一下两点：\n",
    ">>> 1. method = 'POST'\n",
    ">>> 2. action = '对应的页面路由'\n",
    "\n",
    "```\n",
    "<form method='POST'   action='/result'>\n",
    " First name:<br>\n",
    "<input type=\"text\" name=\"firstname\">\n",
    "<br>\n",
    " Last name:<br>\n",
    "<input type=\"text\" name=\"lastname\">\n",
    "<br>\n",
    "<input type=\"submit\" name=\"提交\">\n",
    "</form> \n",
    "```\n",
    "> 4. request\n",
    ">> 1. request.form 接受数据\n",
    ">> 2. HTML 输入框的变量名 ; python 变量名"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0b021742",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on method route in module flask.app:\n",
      "\n",
      "route(rule, **options) method of flask.app.Flask instance\n",
      "    A decorator that is used to register a view function for a\n",
      "    given URL rule.  This does the same thing as :meth:`add_url_rule`\n",
      "    but is intended for decorator usage::\n",
      "    \n",
      "        @app.route('/')\n",
      "        def index():\n",
      "            return 'Hello World'\n",
      "    \n",
      "    For more information refer to :ref:`url-route-registrations`.\n",
      "    \n",
      "    :param rule: the URL rule as string\n",
      "    :param endpoint: the endpoint for the registered URL rule.  Flask\n",
      "                     itself assumes the name of the view function as\n",
      "                     endpoint\n",
      "    :param options: the options to be forwarded to the underlying\n",
      "                    :class:`~werkzeug.routing.Rule` object.  A change\n",
      "                    to Werkzeug is handling of method options.  methods\n",
      "                    is a list of methods this rule should be limited\n",
      "                    to (``GET``, ``POST`` etc.).  By default a rule\n",
      "                    just listens for ``GET`` (and implicitly ``HEAD``).\n",
      "                    Starting with Flask 0.6, ``OPTIONS`` is implicitly\n",
      "                    added and handled by the standard request handling.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(app.route)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "52467a51",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * Serving Flask app \"__main__\" (lazy loading)\n",
      " * Environment: production\n",
      "   WARNING: This is a development server. Do not use it in a production deployment.\n",
      "   Use a production WSGI server instead.\n",
      " * Debug mode: off\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)\n",
      "127.0.0.1 - - [30/Dec/2021 16:29:42] \"\u001b[37mGET / HTTP/1.1\u001b[0m\" 200 -\n",
      "127.0.0.1 - - [30/Dec/2021 16:29:42] \"\u001b[33mGET /docs/5.1/dist/js/bootstrap.bundle.min.js HTTP/1.1\u001b[0m\" 404 -\n"
     ]
    }
   ],
   "source": [
    "from flask import Flask,request\n",
    "from flask import render_template\n",
    "\n",
    "# flask角色： 中央登记中心\n",
    "# 1. 路由 route：\n",
    "# 1.1  url 相对路径：不完整的路径（相对于IP地址的路径，所以IP地址和端口号不需要写）；\n",
    "#    绝对路径是完整路径。\n",
    "# 1.2  request 的请求方法：【GET默认】【POST】\n",
    "# 2  视图函数\n",
    "\n",
    "app = Flask(__name__)\n",
    "\n",
    "# 【GET默认】\n",
    "@app.route(\"/\")\n",
    "def hello_world():\n",
    "    return render_template('hello.html')\n",
    "\n",
    "# 【GET默认】\n",
    "@app.route(\"/index\")\n",
    "def index():\n",
    "    return render_template('index.html')\n",
    "\n",
    "# 【GET默认】\n",
    "@app.route(\"/index_vowels\")\n",
    "def index_vowels():\n",
    "    return render_template('index_vowels.html')\n",
    "\n",
    "# 【GET默认】\n",
    "@app.route(\"/tree\")\n",
    "def tree():\n",
    "    return render_template('tree.html')\n",
    "\n",
    "# 【POST】:用户提交的数据用POST方法---名字全称\n",
    "@app.route(\"/result\",methods=['POST'])\n",
    "def result():\n",
    "    # 我们怎么接受数据？ flask提供了request.form的方法用来\n",
    "    # 接收用户数据\n",
    "    # 第一个项目：名字的全称\n",
    "    py_firstname = request.form['firstname'] \n",
    "    py_lastname = request.form['lastname']\n",
    "    py_fullname = py_firstname.title() + ' ' + py_lastname.title()\n",
    "            \n",
    "    return render_template('result.html',\n",
    "                          fullname = py_fullname\n",
    "                          )\n",
    "\n",
    "# 【POST】:用户提交的数据用POST方法---元音字母\n",
    "@app.route(\"/result_vowels\",methods=['POST'])\n",
    "def result_vowels():\n",
    "    # 我们怎么接受数据？ flask提供了request.form的方法用来\n",
    "    # 接收用户数据\n",
    "    \n",
    "    # 第二个项目：元音字母表词频统计\n",
    "    py_word = request.form['word']\n",
    "    py_vowels = request.form['vowels']\n",
    "    py_found = {}\n",
    "    for i in py_word:\n",
    "        if i in py_vowels:\n",
    "            py_found.setdefault(i,0)\n",
    "            py_found[i]+=1\n",
    "            \n",
    "    return render_template('result_vowels.html',\n",
    "                          found = py_found)\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    app.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f5b6c8a6",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b2c2b7ca",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
